package top.whysu.edevice.controller.system;

import com.feilong.core.Validator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.whysu.edevice.dao.system.SystemOperLogDao;
import top.whysu.edevice.dto.generate.RequestDTO;
import top.whysu.edevice.dto.generate.ResponseDTO;
import top.whysu.edevice.dto.system.AccountDTO;
import top.whysu.edevice.dto.system.CategoryTreeDTO;
import top.whysu.edevice.dto.system.LoginResponseDTO;
import top.whysu.edevice.dto.system.ModifyPwdDTO;
import top.whysu.edevice.service.system.AccountService;
import top.whysu.edevice.service.system.CategoryService;
import top.whysu.edevice.service.system.LoginService;
import top.whysu.edevice.utils.ResponseUtil;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户账户表 前端控制器
 * </p>
 *
 * @author xj
 * @since 2016-12-20
 */
@Controller
public class IndexController {
	private Logger LOGGER = LogManager.getLogger(IndexController.class.getName());

	@Autowired
	SystemOperLogDao systemOperLogDao;
	@Resource
	private LoginService loginService;
	@Resource
	private CategoryService categoryService;
	@Resource
	private AccountService accountService;

	/**
	 * 登录页面
	 */
	@RequestMapping(value="/login",method= RequestMethod.GET)
    public String login(HttpServletResponse response, ServletRequest request, @RequestHeader HttpHeaders header, Map<String, Object> map, String msg){
		Subject subject = SecurityUtils.getSubject();
		if(subject.isAuthenticated()){
			return "redirect:index";
		}
		if(Validator.isNotNullOrEmpty(header.get("X-Requested-With"))){
			response.setHeader("sessionstatus", "timeout");
		}
       return "login";
    }

	/**
	 * 主页面
	 */
	@RequestMapping({"/","/index" })
	public String index(Map<String, Object> map, String page) {
		// 当前用户
		String username = (String) SecurityUtils.getSubject().getPrincipal();
		List<CategoryTreeDTO> treeDTOList = categoryService.selectMenuTree();
		map.put("menus", treeDTOList);
		map.put("username", username);
		map.put("page", page);
		return "index";
	}

	/**
	 * 用户登录
	 *
	 * @param request 请求数据传输对象
	 * @return 应答数据传输对象
	 */
	@ResponseBody
	@PostMapping("/dologin")
	public ResponseDTO<LoginResponseDTO> login(@RequestBody final RequestDTO<AccountDTO> request) {
		LoginResponseDTO login = loginService.login(request.getBody());
		return ResponseUtil.getSuccessResponse(request.getHeader(), login);
	}

	/**
	 * 用户登出
	 *
	 * @return 应答数据传输对象
	 */
	@RequestMapping("/logout")
	public ResponseDTO<String> logout(@RequestBody final RequestDTO<String> request) {
		loginService.logout();
		return ResponseUtil.getSuccessResponse();
	}

	/**
	 * 修改密码
	 *
	 */
	@RequestMapping("/modifyPassword")
	@ResponseBody
	public ResponseDTO<String> modifyPwd(@RequestBody final RequestDTO<ModifyPwdDTO> request) {
		accountService.wholeModifyPwd(request.getBody());
		return ResponseUtil.getSuccessResponse(request.getHeader());
	}
}
